﻿#region Copyright (c) 2012-10, Olaf Kober <amarok.projects@gmail.com>
//================================================================================
//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights
//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//	copies of the Software, and to permit persons to whom the Software is
//	furnished to do so, subject to the following conditions:
//
//	The above copyright notice and this permission notice shall be included in
//	all copies or substantial portions of the Software.
//
//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//	THE SOFTWARE.
//================================================================================
#endregion

#if TESTS

using System;
using System.Collections.Generic;
using NUnit.Framework;


namespace Amarok
{
	[TestFixture]
	public class Test_DictionaryExtensions
	{
		#region AddOrUpdate

		[Test]
		public void Test_AddOrUpdate_AddValue_Update()
		{
			var collection = new Dictionary<Int32, String>();
			collection.Add(111, "111");

			Assert.That(
				collection.AddOrUpdate(111, "AAA", (key, val) => key.ToString() + val),
				Is.EqualTo("111111"));

			Assert.That(collection.Count, Is.EqualTo(1));
			Assert.That(collection[111], Is.EqualTo("111111"));
		}

		[Test]
		public void Test_AddOrUpdate_AddValue_Add()
		{
			var collection = new Dictionary<Int32, String>();
			collection.Add(111, "111");

			Assert.That(
				collection.AddOrUpdate(222, "AAA", (key, val) => key.ToString() + val),
				Is.EqualTo("AAA"));

			Assert.That(collection.Count, Is.EqualTo(2));
			Assert.That(collection[111], Is.EqualTo("111"));
			Assert.That(collection[222], Is.EqualTo("AAA"));
		}

		[Test]
		public void Test_AddOrUpdate_AddValue_NullUpdateFunc()
		{
			var collection = new Dictionary<Int32, String>();
			collection.Add(111, "111");

			Assert.Throws<ArgumentNullException>(() =>
				{
					collection.AddOrUpdate(111, "AAA", null);
				});
		}

		[Test]
		public void Test_AddOrUpdate_AddValueFunc_Update()
		{
			var collection = new Dictionary<Int32, String>();
			collection.Add(111, "111");

			Assert.That(
				collection.AddOrUpdate(111, key => "AAA", (key, val) => key.ToString() + val),
				Is.EqualTo("111111"));

			Assert.That(collection.Count, Is.EqualTo(1));
			Assert.That(collection[111], Is.EqualTo("111111"));
		}

		[Test]
		public void Test_AddOrUpdate_AddValueFunc_Add()
		{
			var collection = new Dictionary<Int32, String>();
			collection.Add(111, "111");

			Assert.That(
				collection.AddOrUpdate(222, key => key.ToString(), (key, val) => key.ToString() + val),
				Is.EqualTo("222"));

			Assert.That(collection.Count, Is.EqualTo(2));
			Assert.That(collection[111], Is.EqualTo("111"));
			Assert.That(collection[222], Is.EqualTo("222"));
		}

		[Test]
		public void Test_AddOrUpdate_AddValueFunc_NullAddValueFunc()
		{
			var collection = new Dictionary<Int32, String>();
			collection.Add(111, "111");

			Assert.Throws<ArgumentNullException>(() =>
				{
					collection.AddOrUpdate(222, (Func<Int32, String>)null, (key, value) => "XXX");
				});
		}

		[Test]
		public void Test_AddOrUpdate_AddValueFunc_NullUpdateValueFunc()
		{
			var collection = new Dictionary<Int32, String>();
			collection.Add(111, "111");

			Assert.Throws<ArgumentNullException>(() =>
				{
					collection.AddOrUpdate(222, key => "AAA", null);
				});
		}

		#endregion

		#region GetOrAdd

		[Test]
		public void Test_GetOrAdd_Value()
		{
			var collection = new Dictionary<Int32, String>();
			collection.Add(111, "111");

			Assert.That(collection.GetOrAdd(111, "AAA"), Is.EqualTo("111"));
			Assert.That(collection.Count, Is.EqualTo(1));
			Assert.That(collection[111], Is.EqualTo("111"));

			Assert.That(collection.GetOrAdd(222, "BBB"), Is.EqualTo("BBB"));
			Assert.That(collection.Count, Is.EqualTo(2));
			Assert.That(collection[111], Is.EqualTo("111"));
			Assert.That(collection[222], Is.EqualTo("BBB"));
		}

		[Test]
		public void Test_GetOrAdd_ValueFunc()
		{
			var collection = new Dictionary<Int32, String>();
			collection.Add(111, "111");

			Assert.That(collection.GetOrAdd(111, key => "AAA"), Is.EqualTo("111"));
			Assert.That(collection.Count, Is.EqualTo(1));
			Assert.That(collection[111], Is.EqualTo("111"));

			Assert.That(collection.GetOrAdd(222, key => key.ToString()), Is.EqualTo("222"));
			Assert.That(collection.Count, Is.EqualTo(2));
			Assert.That(collection[111], Is.EqualTo("111"));
			Assert.That(collection[222], Is.EqualTo("222"));
		}

		[Test]
		public void Test_GetOrAdd_ValueFunc_NullValueFunc()
		{
			var collection = new Dictionary<String, Int32>();

			Assert.Throws<ArgumentNullException>(() =>
				{
					collection.GetOrAdd("AAA", null);
				});
		}

		#endregion

		#region GetValueOrDefault

		[Test]
		public void Test_GetValueOrDefault_String_String()
		{
			var collection = new Dictionary<String, String>();
			collection.Add("A", "AAA");
			collection.Add("B", "BBB");

			Assert.That(collection.GetValueOrDefault("A"), Is.EqualTo("AAA"));
			Assert.That(collection.GetValueOrDefault("B"), Is.EqualTo("BBB"));
			Assert.That(collection.GetValueOrDefault("C"), Is.Null);
		}

		[Test]
		public void Test_GetValueOrDefault_String_Int32()
		{
			var collection = new Dictionary<String, Int32>();
			collection.Add("A", 111);
			collection.Add("B", 222);

			Assert.That(collection.GetValueOrDefault("A"), Is.EqualTo(111));
			Assert.That(collection.GetValueOrDefault("B"), Is.EqualTo(222));
			Assert.That(collection.GetValueOrDefault("C"), Is.EqualTo(0));
		}

		[Test]
		public void Test_GetValueOrDefault_DefaultValue_String_String()
		{
			var collection = new Dictionary<String, String>();
			collection.Add("A", "AAA");
			collection.Add("B", "BBB");

			Assert.That(collection.GetValueOrDefault("A", "DEF"), Is.EqualTo("AAA"));
			Assert.That(collection.GetValueOrDefault("B", "DEF"), Is.EqualTo("BBB"));
			Assert.That(collection.GetValueOrDefault("C", "DEF"), Is.EqualTo("DEF"));
			Assert.That(collection.GetValueOrDefault("D", null), Is.Null);
			Assert.That(collection.GetValueOrDefault("E", ""), Is.EqualTo(String.Empty));
		}

		[Test]
		public void Test_GetValueOrDefault_DefaultValue_String_Int32()
		{
			var collection = new Dictionary<String, Int32>();
			collection.Add("A", 111);
			collection.Add("B", 222);

			Assert.That(collection.GetValueOrDefault("A", 999), Is.EqualTo(111));
			Assert.That(collection.GetValueOrDefault("B", 999), Is.EqualTo(222));
			Assert.That(collection.GetValueOrDefault("C", 999), Is.EqualTo(999));
		}

		#endregion

	}
}

#endif
